package com.shensi.adminCollect.service.station.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.shensi.common.core.exception.BaseException;
import com.shensi.common.core.utils.StringUtils;
import com.shensi.adminCollect.domain.station.CardBatch;
import com.shensi.adminCollect.domain.station.Enterprise;
import com.shensi.adminCollect.domain.station.Station;
import com.shensi.adminCollect.mapper.station.CardBatchMapper;
import com.shensi.adminCollect.mapper.station.PhysicalCardMapper;
import com.shensi.adminCollect.service.station.CardBatchService;
import com.shensi.adminCollect.service.station.IEnterpriseService;
import com.shensi.adminCollect.service.station.IStationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service("cardBatchService")
@Slf4j
public class CardBatchServiceImpl implements CardBatchService {

    @Resource
    private CardBatchMapper cardBatchMapper;

    @Resource
    private PhysicalCardMapper physicalCardMapper;

    @Autowired
    private IEnterpriseService enterpriseService;

    @Autowired
    private IStationService stationService;

    @Override
    public int saveCardBatch(CardBatch cardBatch) {
        if(Objects.isNull(cardBatch)){
            throw new BaseException("请求参数为空");
        }
        if(StringUtils.isEmpty(cardBatch.getBatchNo())){
            throw new BaseException("请求参数没有批次号");
        }
        if(StringUtils.isEmpty(cardBatch.getPhysicalCardType())){
            throw new BaseException("请求参数没有选择实体卡类型");
        }
        CardBatch newCardBatch = cardBatchMapper.selectByPrimaryKey(cardBatch.getBatchNo(),cardBatch.getPhysicalCardType());
        if(Objects.nonNull(newCardBatch)){
            throw new BaseException("批次号已存在");
        }
        // 定额卡需要一开始配置油企油站  储值卡不会
        if(Objects.nonNull(cardBatch.getEnterpriseId())){
            Enterprise enterprise = enterpriseService.selectEnterpriseById(cardBatch.getEnterpriseId());
            cardBatch.setEnterpriseName(Objects.nonNull(enterprise) ? enterprise.getEnterpriseName() : null);
        }
        if(StringUtils.isNotEmpty(cardBatch.getStationId())){
            cardBatch.setStationName(getStationName(cardBatch.getStationId()));
        }

        return cardBatchMapper.insertSelective(cardBatch);
    }

    private String getStationName(String stationIds){
        String[] strings = stationIds.split(",");
        String stationName = "";
        for (String s : strings){
            Station station = stationService.selectStationById(Long.valueOf(s));
            stationName = stationName + station.getStationName() + ",";
        }
        stationName = stationName.substring(0,stationName.length() - 1);
        return stationName;
    }

    @Override
    public List<CardBatch> selectCardBatchList(CardBatch cardBatch) {
        List<CardBatch> cardBatchList = cardBatchMapper.selectCardBatchList(cardBatch);
        if(CollectionUtil.isEmpty(cardBatchList)){
            return null;
        }
        for (CardBatch batch : cardBatchList) {
            if(Objects.nonNull(batch)){
                int num = physicalCardMapper.selectPhysicalCardNumByBatchId(batch.getId().toString());
                batch.setTotalNum(num);
            }
        }
        return cardBatchList;
    }

    @Override
    public int updateCardBatch(CardBatch cardBatch) {
        if(Objects.isNull(cardBatch)){
            throw new BaseException("请求参数为空");
        }
        if(StringUtils.isEmpty(cardBatch.getBatchNo())){
            throw new BaseException("请求参数没有批次号");
        }
        return cardBatchMapper.updateByPrimaryKeySelective(cardBatch);
    }

    @Override
    public int deleteCardBatch(Long id) {
        return cardBatchMapper.updateCardBatch(id);
    }

    @Override
    public CardBatch selectCardBatchLastByType(CardBatch cardBatch) {
        return cardBatchMapper.selectCardBatchLastByType(cardBatch.getPhysicalCardType());
    }

    @Override
    public CardBatch selectCardBatchById(Long id) {

        return cardBatchMapper.selectCardBatchById(id);
    }

    @Override
    public CardBatch selectCardBatch(String batchNo, String physicalCardType) {
        return cardBatchMapper.selectByPrimaryKey(batchNo,physicalCardType);
    }

}
